package com.hxzy.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.hxzy.common.util.WaimaiThreadLocal;
import com.hxzy.vo.LoginVO;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 * mybatis-plus在新增和修改的时候，对实体某些字段进行数据填充
 * @TableField(.. fill = FieldFill.INSERT)
 * @TableField(.. fill = FieldFill.UPDATE)
 * @author Administrator
 */
@Component
public class MyBatisPlusMetaObjectHandler implements MetaObjectHandler {

    /**
     * 新增(insert方法的时候)
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        LoginVO current = WaimaiThreadLocal.getCurrent();
        if(current!=null) {
            this.strictInsertFill(metaObject, "merchantId", Long.class, current.getAccountVO().getId());
            this.strictInsertFill(metaObject, "createBy", String.class, current.getAccountVO().getLoginName());
        }

        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
    }

    /**
     * 修改(update方法的时候)
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        LoginVO current = WaimaiThreadLocal.getCurrent();
        if(current!=null) {
            this.strictInsertFill(metaObject, "updateBy", String.class, current.getAccountVO().getLoginName());
        }

        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
    }
}
